package com.briup.homework.chap11;

import java.io.*;

public class Test027_MultipleCopy {
    public static void main(String[] args) {
        // 源⽂件所在路径
        String dirPath = "D:/test";
        // 源⽂件名称
        String fileName = "img.jpg";
        // 对于文件的名字做处理
        String[] split = fileName.split("[.]");
        String n =  split[0];
        String e = split[1];

        // 复制数量
        int number = 10;
        // 完成剩余代码编写
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            fis = new FileInputStream(new File(dirPath,fileName));
            for(int i = 1; i <= number; i++){
                String newFileName = n+i+"."+e;
                fos = new FileOutputStream(new File(dirPath,newFileName));
                bis = new BufferedInputStream(fis);
                bos = new BufferedOutputStream(fos);
                int len = -1;
                byte[] buf = new byte[1024];
                while((len = bis.read(buf)) != -1){
                    bos.write(buf,0,len);
                }
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        } finally {
            if(bos!=null){
                try {
                    bos.close();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
            if(bis!=null){
                try {
                    bis.close();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }
        System.out.println("十个图片复制成功！");

    }
}
